package com.hjk.location_upload_flutter;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocationClient;
import com.hdgq.locationlib.LocationOpenApi;
import com.hdgq.locationlib.entity.ShippingNoteInfo;
import com.hdgq.locationlib.listener.OnResultListener;
import com.hdgq.locationlib.listener.OnSendResultListener;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** LocationUploadFlutterPlugin */
public class LocationUploadFlutterPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Context context;
  private Application gottenApplication;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "location_upload_flutter");
    context = flutterPluginBinding.getApplicationContext();
    channel.setMethodCallHandler(this);
  }

  public static void initService(Application application) {
      Log.d("TAG", "注册");
      LocationOpenApi.init(application);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if (call.method.equals("setApiKey")) {
        AMapLocationClient.setApiKey((String) call.argument("android"));
    } else if (call.method.equals("auth")) {
        AMapLocationClient.updatePrivacyShow(context, true, true);
        AMapLocationClient.updatePrivacyAgree(context, true);

      ///认证
      String appId =  call.argument("appId");
      String appSecurity =  call.argument("appSecurity");
      String enterpriseSenderCode =  call.argument("enterpriseSenderCode");
      String environment =  call.argument("environment");
      LocationOpenApi.auth(context, appId, appSecurity,
              enterpriseSenderCode, environment, new OnResultListener() {

                @Override
                public void onFailure(String s, String s1) {
                    Log.d("TAG", "android" + s);
                    Log.d("TAG", "android" + s1);
                  Map map = new HashMap();
                  map.put("code", s);
                  map.put("message", s1);
                  result.success(map);
                }

                @Override
                public void onSuccess(List<ShippingNoteInfo> list) {
                    Log.d("TAG", "android" + "auth成功");
                    Log.d("android","initKepler"+list);

                    List<Map> shippingNoteInfos = new LinkedList();
                    if (list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            ShippingNoteInfo shippingNoteInfo = list.get(i);
                            Map map = new HashMap();
                            map.put("interval", shippingNoteInfo.getInterval());
                            map.put("sendCount", shippingNoteInfo.getSendCount());
                            map.put("serialNumber", shippingNoteInfo.getSerialNumber());
                            map.put("shippingNoteNumber", shippingNoteInfo.getShippingNoteNumber());
                            shippingNoteInfos.add(map);
                        }
                    }
                    Map map = new HashMap();
                   map.put("code", 0);
                   map.put("data", shippingNoteInfos);
                  result.success(map);
                }
              });
    } else if (call.method.equals("start")) {

        AMapLocationClient.updatePrivacyShow(context, true, true);
        AMapLocationClient.updatePrivacyAgree(context, true);

        ///启运
        String vehicleNumber =  call.argument("vehicleNumber");
        String driverName =  call.argument("driverName");
        List<Map> lists =  call.argument("shippingNoteInfos");
        String remark =  call.argument("remark");

        List<ShippingNoteInfo> shippingNoteInfos = new LinkedList();

        for (int i = 0; i < lists.size(); i++) {
            Map shippingMap = lists.get(i);
            ShippingNoteInfo shippingNoteInfo = new ShippingNoteInfo();
            shippingNoteInfo.setShippingNoteNumber((String) shippingMap.get("shippingNoteNumber"));
            shippingNoteInfo.setSerialNumber((String) shippingMap.get("serialNumber"));
            shippingNoteInfo.setStartCountrySubdivisionCode((String) shippingMap.get("startCountrySubdivisionCode"));
            shippingNoteInfo.setEndCountrySubdivisionCode((String) shippingMap.get("endCountrySubdivisionCode"));
            shippingNoteInfo.setStartLongitude(Double.valueOf((String) shippingMap.get("startLongitude")));
            shippingNoteInfo.setStartLatitude(Double.valueOf((String) shippingMap.get("startLatitude")));
            shippingNoteInfo.setEndLongitude(Double.valueOf((String) shippingMap.get("endLongitude")));
            shippingNoteInfo.setEndLatitude(Double.valueOf((String) shippingMap.get("endLatitude")));
            shippingNoteInfo.setStartLocationText((String) shippingMap.get("startLocationText"));
            shippingNoteInfo.setEndLocationText((String) shippingMap.get("endLocationText"));
            shippingNoteInfo.setVehicleNumber((String) shippingMap.get("vehicleNum"));
            shippingNoteInfo.setDriverName((String) shippingMap.get("driverName"));
            shippingNoteInfos.add(shippingNoteInfo);
        }

       LocationOpenApi.start(context, vehicleNumber, driverName, remark, shippingNoteInfos.toArray(new ShippingNoteInfo[lists.size()]), new OnResultListener() {
           @Override
           public void onFailure(String s, String s1) {
               Log.d("TAG", "android" +s);
               Log.d("TAG", "android" +s1);
               Map map = new HashMap();
               map.put("code", s);
               map.put("message", s1);
               result.success(map);
           }

           @Override
           public void onSuccess(List<ShippingNoteInfo> list) {
               Log.d("TAG", "android" +"start成功");
               Log.d("flutter-taoke","android"+list);

               List<Map> shippingNoteInfos = new LinkedList();
               if (list.size() > 0) {
                   for (int i = 0; i < list.size(); i++) {
                       ShippingNoteInfo shippingNoteInfo = list.get(i);
                       Map map = new HashMap();
                       map.put("interval", shippingNoteInfo.getInterval());
                       map.put("sendCount", shippingNoteInfo.getSendCount());
                       map.put("serialNumber", shippingNoteInfo.getSerialNumber());
                       map.put("shippingNoteNumber", shippingNoteInfo.getShippingNoteNumber());
                       shippingNoteInfos.add(map);
                   }
               }

               Map map = new HashMap();
               map.put("code", 0);
               map.put("data", shippingNoteInfos);
               result.success(map);
           }
       });

    } else if (call.method.equals("send")) {

        AMapLocationClient.updatePrivacyShow(context, true, true);
        AMapLocationClient.updatePrivacyAgree(context, true);

        ///位置信息上报
        String vehicleNumber =  call.argument("vehicleNumber");
        String driverName =  call.argument("driverName");
        List<Map> lists =  call.argument("shippingNoteInfos");
        String remark =  call.argument("remark");

        List<ShippingNoteInfo> shippingNoteInfos = new LinkedList();
        for (int i = 0; i < lists.size(); i++) {
            Map shippingMap = lists.get(i);
            ShippingNoteInfo shippingNoteInfo = new ShippingNoteInfo();
            shippingNoteInfo.setShippingNoteNumber((String) shippingMap.get("shippingNoteNumber"));
            shippingNoteInfo.setSerialNumber((String) shippingMap.get("serialNumber"));
            shippingNoteInfo.setStartCountrySubdivisionCode((String) shippingMap.get("startCountrySubdivisionCode"));
            shippingNoteInfo.setEndCountrySubdivisionCode((String) shippingMap.get("endCountrySubdivisionCode"));
            shippingNoteInfo.setVehicleNumber((String) shippingMap.get("vehicleNum"));
            shippingNoteInfo.setDriverName((String) shippingMap.get("driverName"));
            shippingNoteInfo.setInterval(Long.valueOf((String) shippingMap.get("interval")));
            shippingNoteInfos.add(shippingNoteInfo);
        }

        LocationOpenApi.send(context, vehicleNumber, driverName, remark, shippingNoteInfos.toArray(new ShippingNoteInfo[lists.size()]), new OnSendResultListener() {
            @Override
            public void onFailure(String s, String s1, List<ShippingNoteInfo> list) {
                Map map = new HashMap();
                map.put("code", s);
                map.put("data", list);
                map.put("message", s1);
                result.success(map);
            }

            @Override
            public void onSuccess(List<ShippingNoteInfo> list) {

                List<Map> shippingNoteInfos = new LinkedList();
                if (list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        ShippingNoteInfo shippingNoteInfo = list.get(i);
                        Map map = new HashMap();
                        map.put("interval", shippingNoteInfo.getInterval());
                        map.put("sendCount", shippingNoteInfo.getSendCount());
                        map.put("serialNumber", shippingNoteInfo.getSerialNumber());
                        map.put("shippingNoteNumber", shippingNoteInfo.getShippingNoteNumber());
                        shippingNoteInfos.add(map);
                    }
                }

                Map map = new HashMap();
                map.put("code", 0);
                map.put("data", shippingNoteInfos);
                result.success(map);
            }
        });

    } else if (call.method.equals("pause")) {

        AMapLocationClient.updatePrivacyShow(context, true, true);
        AMapLocationClient.updatePrivacyAgree(context, true);
        ///位置信息上报
        String vehicleNumber =  call.argument("vehicleNumber");
        String driverName =  call.argument("driverName");
        List lists =  call.argument("shippingNoteInfos");
        String remark =  call.argument("remark");

        List<ShippingNoteInfo> shippingNoteInfos = new LinkedList();
        for (int i = 0; i < lists.size(); i++) {
            ShippingNoteInfo shippingNoteInfo = new ShippingNoteInfo();
            shippingNoteInfos.add(shippingNoteInfo);
        }

        LocationOpenApi.pause(context, vehicleNumber, driverName, remark, shippingNoteInfos.toArray(new ShippingNoteInfo[lists.size()]), new OnResultListener() {
            @Override
            public void onFailure(String s, String s1) {
                Map map = new HashMap();
                map.put("code", s);
                map.put("message", s1);
                result.success(map);
            }

            @Override
            public void onSuccess(List<ShippingNoteInfo> list) {
                Map map = new HashMap();
                map.put("code", 0);
                map.put("data", list);
                result.success(map);
            }
        });

    } else if (call.method.equals("restart")) {

        AMapLocationClient.updatePrivacyShow(context, true, true);
        AMapLocationClient.updatePrivacyAgree(context, true);

        ///位置信息上报
        String vehicleNumber =  call.argument("vehicleNumber");
        String driverName =  call.argument("driverName");
        List lists =  call.argument("shippingNoteInfos");
        String remark =  call.argument("remark");

        List<ShippingNoteInfo> shippingNoteInfos = new LinkedList();
        for (int i = 0; i < lists.size(); i++) {
            ShippingNoteInfo shippingNoteInfo = new ShippingNoteInfo();
            shippingNoteInfos.add(shippingNoteInfo);
        }

        LocationOpenApi.restart(context, vehicleNumber, driverName, remark, shippingNoteInfos.toArray(new ShippingNoteInfo[lists.size()]), new OnResultListener() {
            @Override
            public void onFailure(String s, String s1) {
                Map map = new HashMap();
                map.put("code", s);
                map.put("message", s1);
                result.success(map);
            }

            @Override
            public void onSuccess(List<ShippingNoteInfo> list) {
                Map map = new HashMap();
                map.put("code", 0);
                map.put("data", list);
                result.success(map);
            }
        });

    } else if (call.method.equals("stop")) {

        AMapLocationClient.updatePrivacyShow(context, true, true);
        AMapLocationClient.updatePrivacyAgree(context, true);

        ///位置信息上报
        String vehicleNumber =  call.argument("vehicleNumber");
        String driverName =  call.argument("driverName");
        List<Map> lists =  call.argument("shippingNoteInfos");
        String remark =  call.argument("remark");

        List<ShippingNoteInfo> shippingNoteInfos = new LinkedList();
        for (int i = 0; i < lists.size(); i++) {
            Map shippingMap = lists.get(i);
            ShippingNoteInfo shippingNoteInfo = new ShippingNoteInfo();
            shippingNoteInfo.setShippingNoteNumber((String) shippingMap.get("shippingNoteNumber"));
            shippingNoteInfo.setSerialNumber((String) shippingMap.get("serialNumber"));
            shippingNoteInfo.setStartCountrySubdivisionCode((String) shippingMap.get("startCountrySubdivisionCode"));
            shippingNoteInfo.setEndCountrySubdivisionCode((String) shippingMap.get("endCountrySubdivisionCode"));
            shippingNoteInfo.setStartLongitude(Double.valueOf((String) shippingMap.get("startLongitude")));
            shippingNoteInfo.setStartLatitude(Double.valueOf((String) shippingMap.get("startLatitude")));
            shippingNoteInfo.setEndLongitude(Double.valueOf((String) shippingMap.get("endLongitude")));
            shippingNoteInfo.setEndLatitude(Double.valueOf((String) shippingMap.get("endLatitude")));
            shippingNoteInfo.setStartLocationText((String) shippingMap.get("startLocationText"));
            shippingNoteInfo.setEndLocationText((String) shippingMap.get("endLocationText"));
            shippingNoteInfo.setVehicleNumber((String) shippingMap.get("vehicleNum"));
            shippingNoteInfo.setDriverName((String) shippingMap.get("driverName"));
            shippingNoteInfos.add(shippingNoteInfo);
        }

        LocationOpenApi.stop(context, vehicleNumber, driverName, remark, shippingNoteInfos.toArray(new ShippingNoteInfo[lists.size()]), new OnResultListener() {
            @Override
            public void onFailure(String s, String s1) {
                Map map = new HashMap();
                map.put("code", s);
                map.put("message", s1);
                result.success(map);
            }

            @Override
            public void onSuccess(List<ShippingNoteInfo> list) {
                Map map = new HashMap();
                map.put("code", 0);
                map.put("data", list);
                result.success(map);
            }
        });

    } else {
      result.notImplemented();
    }
  }


  


  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
